package cn.dapeng.rpc.common.cb;

import cn.dapeng.rpc.common.RpcResponse;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class ResponseCallBackV2 {


    /**
     * key : requestId
     */
    private final static ConcurrentHashMap<Long, CompletableFuture<Object>> mapping = new ConcurrentHashMap<>();

    public static void addCallback(Long requestId, CompletableFuture future) {
        mapping.putIfAbsent(requestId, future);
    }

    public static void runCallback(RpcResponse result) {
        mapping.get(result.getRequestId()).complete(result);
        removeCallback(result.getRequestId());
    }

    public static void removeCallback(Long requestId) {
        mapping.remove(requestId);
    }


}
